package com.hanson.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanson.Const;
import com.hanson.common.service.RedisService;
import com.hanson.common.util.BusinessUtil;
import com.hanson.entity.UserBean;
import com.hanson.mapper.UserBeanMapper;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
@CacheConfig(cacheNames = "UserData")
public class UserService {
    @Autowired
    private UserBeanMapper userBeanMapper;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private RedisService redisService;

    public Page<UserBean> listUsers(Page<UserBean> page, UserBean userBean) {
        List<UserBean> list = userBeanMapper.listUsers(page, userBean);
        if (!CollectionUtils.isEmpty(list)) {
            list.stream().forEach(c -> {
                c.setWebMoney(BusinessUtil.getWebAmount(c.getMoney()));
            });
        }
        return page.setRecords(list);
    }

    /**
     * 修改用户信息
     *
     * @param userBean
     */
    public void updateUser(UserBean userBean) {
        UserBean returnBean = userBeanMapper.selectByPrimaryKey(userBean.getId());
        if (null != returnBean) {
            returnBean.setUpdateDate(new Date());
            returnBean.setUserName(userBean.getUserName());
            returnBean.setPhone(userBean.getPhone());
            returnBean.setPassword(userBean.getPassword());
            userBeanMapper.updateByPrimaryKeySelective(returnBean);
            redisService.del(String.format(Const.USER_REDIS_KEY, returnBean.getOpenId()));
        }
    }

    /**
     * 新增用户
     */
    public void addUser(WxMpUser wxMpUser) {
        UserBean userBean = new UserBean();
        userBean.setCreateDate(new Date());
        userBean.setUserType("1");
        userBean.setGender(wxMpUser.getSex());
        userBean.setCountry(wxMpUser.getCountry());
        userBean.setProvince(wxMpUser.getProvince());
        userBean.setCity(wxMpUser.getCity());
        userBean.setHeadImg(wxMpUser.getHeadImgUrl());
        userBean.setOpenId(wxMpUser.getOpenId());
        userBeanMapper.insert(userBean);
        if (!StringUtils.isEmpty(wxMpUser.getNickname())) {
            String nickNameSql = "update t_user set nick_name=?,user_name=? where open_id=?";
            jdbcTemplate.execute("set names utf8mb4");
            jdbcTemplate.update(nickNameSql, wxMpUser.getNickname(), wxMpUser.getNickname(), userBean.getOpenId());
        }
    }

    @Cacheable(key = "'List:'+#openId", unless = "#result == null")
    public UserBean selectByOpenId(String openId) {
        UserBean userBean = userBeanMapper.selectByOpenId(openId);
        if (null != userBean) {
            userBean.setWebMoney(BusinessUtil.getWebAmount(userBean.getMoney()));
        }
        return userBean;
    }

    public UserBean selectById(String id) {
        UserBean userBean = userBeanMapper.selectByPrimaryKey(Integer.parseInt(id));
        if (null != userBean) {
            userBean.setWebMoney(BusinessUtil.getWebAmount(userBean.getMoney()));
        }
        return userBean;
    }

    public UserBean getByNameAndPwd(String userName, String password) {
        List<UserBean> list = userBeanMapper.selectByUserNameAndPwd(userName, password);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    public int statisUser(int type) {
        return userBeanMapper.statisUser(type);
    }
}
